/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class GlobalConfig {

    public static final int BASE_VALUE = 1000;
    public static final int LOOP_I_MAX_600000 = 600000;
    public static final int NUMBER_100000 = 100000;
    public static final int EXECUTION_MAX_80 = 80;

    public static boolean inDebug = false;
    public static void log(String str) {
        if (inDebug) {
            System.out.println(str);
        }
    }
    public static double currentTimestamp13() {
        return (double) System.nanoTime() / 1_000_000;
    }
}
/*
 * @State
 * @Tags Jetstream2
 */
class Benchmark {
    /*
     * @Benchmark
     */
    int run() {
        long bitwiseAndValue = 4294967296L;
        for (int i = 0; i < GlobalConfig.LOOP_I_MAX_600000; i++) {
            bitwiseAndValue = bitwiseAndValue & i;
            if (GlobalConfig.inDebug && i % GlobalConfig.NUMBER_100000 == 0) {
                GlobalConfig.log(String.format("bitops-bitwise-and: i = %d bitwiseAndValue = %d", i, bitwiseAndValue));
            }
        }

        long result = bitwiseAndValue;
        int expected = 0;
        if (result != expected) {
            throw new Error("ERROR: bad result: expected " + expected + " but got " + result);
        }

        return 1;
    }

    public static void main(String[] args) {
        double startTime = GlobalConfig.currentTimestamp13();
        Benchmark benchmark = new Benchmark();
        for (int i = 0; i < GlobalConfig.EXECUTION_MAX_80; i++) {
            double startTimeInLoop = GlobalConfig.currentTimestamp13();
            int a = benchmark.run();
            double endTimeInLoop = GlobalConfig.currentTimestamp13();
            //GlobalConfig.log(`bitops-bitwise-and: ms = ${endTimeInLoop - startTimeInLoop} i = ${i}`)
        }
        double endTime = GlobalConfig.currentTimestamp13();
        System.out.println("bitops-bitwise-and: ms = " + (endTime - startTime));
    }
}
